package main;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.io.IOException;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import gui.CustomJButton;
import gui.CustomJFrame;
import gui.CustomJPanel;
import gui.IconListPanel;

public class Main {
	/**
	 * Class: Main
	 * Description: entry point
	 * @category main
	 * @author Mr3chi
	 * @since 04/04/2013
	 * 
	 */
	
	public static void main(String[] args) {
		
		final CustomJFrame myFrame = new CustomJFrame();
		myFrame.setSize(new Dimension(500,350));
		CustomJPanel mainPanel = new CustomJPanel(myFrame, 40, 18);
		mainPanel.setLayout(new BorderLayout());
		
		JPanel topPanel, bottomPanel, topLeft, topRight;
		
		topPanel = new JPanel(new BorderLayout());
		topPanel.setOpaque(false);
		
		topLeft = new JPanel(new FlowLayout(FlowLayout.LEFT));
		topLeft.setOpaque(false);
		
		topRight = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		topRight.setOpaque(false);
		
		topPanel.add(topLeft, BorderLayout.WEST);
		topPanel.add(topRight, BorderLayout.EAST);
		
		bottomPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		bottomPanel.setOpaque(false);
		
		try {
			CustomJButton lockerButton = new CustomJButton(DirString.LOCKER_ICON, 
					DirString.LOCKER_HOVER, DirString.LOCKER_CLICK, 20, 20, "Giữ chặt");
			CustomJButton keyButton = new CustomJButton(DirString.KEY_ICON, 
					DirString.KEY_HOVER, DirString.KEY_CLICK, 20, 20, "Khoá bảo mật");
			CustomJButton sortButton = new CustomJButton(DirString.SORT_ICON, 
					DirString.SORT_HOVER, DirString.SORT_CLICK, 20, 20, "Sắp xếp");
			CustomJButton settingButton = new CustomJButton(DirString.SETTING_ICON, 
					DirString.SETTING_HOVER, DirString.SETTING_CLICK, 20, 20, "Cài đặt");
			CustomJButton newButton = new CustomJButton(DirString.NEW_ICON, 
					DirString.NEW_HOVER, DirString.NEW_ICON, 20, 20, "Cài đặt");
			
			topRight.add(lockerButton);
			topRight.add(keyButton);
			topLeft.add(newButton);
			bottomPanel.add(sortButton);
			bottomPanel.add(settingButton);
		} catch (IOException e) {
			JOptionPane.showMessageDialog(myFrame, "Quá trình tạo nút không thành công",
					"Lỗi", JOptionPane.ERROR_MESSAGE, null);
		}
		
		//iconListpanel
		final IconListPanel iconList;
		mainPanel.setPreferredSize(new Dimension(500, 400));
		iconList = new IconListPanel(myFrame);
		JScrollPane scrollPane = new JScrollPane(iconList,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		scrollPane.setOpaque(false);
		scrollPane.getViewport().setOpaque(false);
		scrollPane.setViewportView(iconList);
		scrollPane.setBorder(null);
		scrollPane.getVerticalScrollBar().setUnitIncrement(30);
		
		mainPanel.add(scrollPane,BorderLayout.CENTER);
		
		mainPanel.add(topPanel,BorderLayout.NORTH);
		mainPanel.add(bottomPanel,BorderLayout.SOUTH);
		myFrame.add(mainPanel, BorderLayout.CENTER);
		
		myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		myFrame.pack();
		try {
			iconList.setDirString("D:/");
		} catch (IOException e) {
			e.printStackTrace();
		}
		myFrame.addComponentListener(new CustomComponentLister(iconList));
		myFrame.setVisible(true);
		final String dir = System.getProperty("user.dir");
        System.out.println("current dir = " + dir);
	}
	
	public static final class DirString{
		public static final String KEY_ICON = "ICON/Key_icon.png";
		public static final String KEY_HOVER = "ICON/Key_hover.png";
		public static final String KEY_CLICK = "ICON/Key_click.png";
		
		public static final String LOCKER_ICON = "ICON/Locker_icon.png";
		public static final String LOCKER_HOVER = "ICON/Locker_hover.png";
		public static final String LOCKER_CLICK = "ICON/Locker_click.png";
		
		public static final String SETTING_ICON = "ICON/Setting_icon.png";
		public static final String SETTING_HOVER = "ICON/Setting_hover.png";
		public static final String SETTING_CLICK = "ICON/Setting_click.png";
		
		public static final String SORT_ICON = "ICON/Sort_icon.png";
		public static final String SORT_HOVER = "ICON/Sort_hover.png";
		public static final String SORT_CLICK = "ICON/Sort_click.png";
		
		public static final String NEW_ICON = "ICON/New_icon.png";
		public static final String NEW_HOVER = "ICON/New_hover.png";
	}

}

class CustomComponentLister implements ComponentListener{
	IconListPanel iconlist;
	public CustomComponentLister(IconListPanel iconlist) {
		this.iconlist = iconlist;
	}
	@Override
	public void componentHidden(ComponentEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void componentMoved(ComponentEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void componentResized(ComponentEvent arg0) {
		// TODO Auto-generated method stub
		iconlist.repaintCurrentIcons();
		iconlist.setPreferredSize(new Dimension(iconlist.getRealWidth(), iconlist.getRealHeight()));
	}

	@Override
	public void componentShown(ComponentEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	
}
